package com.dom;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 作者: LCG
 * 日期: 2020/4/25 21:13
 * 描述:
 */
public class DocumentUtil {

    public static Document getDoument() throws ParserConfigurationException {

        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        //从DOM工厂中获得DOM解析器
        DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
        //创建一个文档
        Document document = dbBuilder.newDocument();
        //创建LOGINUSER节点
        Element loginUser = document.createElement("LOGINUSER");

        //创建LOGINUSER节点的子节点 USERINFO节点的需要有多个节点的话 那就循环创建多个
        Element userinfo = document.createElement("USERINFO");
        //创建userINFO的子节点LDAPID CORTORATION SUBCOMPANY BUREAU DEPARTMENT ISLDAPID NAME
        Element ldapid = document.createElement("LDAPID");
        //给创建的元素之间添加文本数据
        ldapid.setTextContent("1111111");
        Element cortoration = document.createElement("CORTORATION");
        cortoration.setTextContent("江苏电力公司");
        //这里给举例怎么给元素添加属性比如给 cortoration添加一个 name属性
        cortoration.setAttribute("name","公司名称");

        Element subcompany = document.createElement("SUBCOMPANY");
        subcompany.setTextContent("XXX子公司");
        Element bureau = document.createElement("BUREAU");
        bureau.setTextContent("县级的单位名称");
        Element department = document.createElement("DEPARTMENT");
        department.setTextContent("XXX部门");
        Element isldapid = document.createElement("ISLDAPID");
        isldapid.setTextContent("1");
        Element name = document.createElement("NAME");
        name.setTextContent("张三");
        //添加userinfo的子节点
        userinfo.appendChild(ldapid);
        userinfo.appendChild(cortoration);
        userinfo.appendChild(subcompany);
        userinfo.appendChild(bureau);
        userinfo.appendChild(isldapid);
        userinfo.appendChild(department);
        userinfo.appendChild(name);

        //将userinfo加入到LOGINUSER跟节点
        loginUser.appendChild(userinfo);

        document.appendChild(loginUser);

        return document;
    }


    /**
     * 将Docment转变为 String字符串
     * @param document
     * @return
     */
    public static String getDocumentString(Document document){
        String xmlStr = "";
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer t = tf.newTransformer();
            // 解决中文问题
            t.setOutputProperty("encoding", "UTF-8");

            t.transform(new DOMSource(document), new StreamResult(bos));
            xmlStr = bos.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return xmlStr;
    }
}
